package day3;

import java.util.ArrayList;
import java.util.List;

public class ch2 {

    public List<Integer> spiralOrder(int[][] matrix) {

        List<Integer> list = new ArrayList<>();
        int[][] dir = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};

        list.add(matrix[0][0]);
        matrix[0][0]=200;

        int index = 0;
        int m = 0;
        int n = 0;
        int k = matrix.length*matrix[0].length;
        for (int i = 1; i < k; i++) {
            int x = m+dir[index][0];
            int y = n+dir[index][1];
            while (!fun(matrix,x,y)){
                index = (index+1)%4;
                x = m+dir[index][0];
                y = n+dir[index][1];
            }
            m = x;
            n = y;
            list.add(matrix[m][n]);
        }

        return list;

    }

    public boolean fun(int[][] matrix,int m,int n){
        if(m<0||m>=matrix.length){
            return false;
        }
        if(n<0||n>=matrix[0].length){
            return false;
        }
        return matrix[m][n] <= 100;
    }


    public static void main(String[] args) {

    }
}
